package com.robusta.spreadsheet.describe.jaxb.validation;

import com.robusta.spreadsheet.describe.Validatable;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlType;
import java.util.regex.Pattern;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType
public class RegexValidator extends BaseValidator {
    @XmlAttribute(required = true)
    protected String pattern;

    @Override
    public void validate(Validatable validatable) throws ValidationException {
        if(!Pattern.compile(pattern).matcher(validatable.getInput()).find()) {
            throw new ValidationException(String.format("Input does not comply with pattern: %s", pattern), validatable);
        }
    }
}
